window.Me = {
  logged_in: false,
  friends: new Array(),
  my_marker: null,
  my_info: null,
  friends_findById: (id) ->
    for friend in Me.friends
      if friend.id is id
        return friend
    return false
  ,
  fallback_init: ->
    setTimeout( ->
      unless Websocket.connected
        Geo.raiseNotice('Esto está tardando más de lo habitual. Si no avanza, refresca la página', 'notice')
        setTimeout( ->
          unless Websocket.connected
            Geo.raiseNotice('Lo siento. El tiempo de espera se agotó y volveré a intentar conectarme en unos segundos. Asegúrate de que la conexión a internet esté activa y puedas utilizar el puerto 8080.')
            setTimeout( ->
              unless Websocket.connected
                window.location = "/"
              else
                Geo.raiseNotice('Al parecer estás conectado ya.', 'notice')
            , 5000)
          else
            Geo.raiseNotice('Al parecer estás conectado ya.', 'notice')
        , 7000)
    ,7000)
  init: ->
    if Me.logged_in
      $.getJSON('/me', (data) ->
        Me.fallback_init()
        Me.my_info = data
        Websocket.init()
        Geoposition.init()
      )
}

window.Map = {
  # my_marker: null,                    # changed to Me object
  panToActualPosition: true,
  panFirstTime: true,
  node: "",
  map: null,
  mapOptions: {
    zoom: 6,
    center: new google.maps.LatLng( -3.7833 ,-78.6167 ),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  },
  do_panToActualPosition: ->
    latlng = new google.maps.LatLng( Position.latitude, Position.longitude )
    if Map.panToActualPosition
      Map.map.panTo(latlng)
    if Map.panFirstTime                             # if first time, create my marker
      Me.my_marker = new MapObjects.Person( Me.my_info , Position )
      # look closer:
      setTimeout( ->
        Map.map.setZoom(16)
      , 1000)
      Map.panFirstTime = false
    unless Map.panFirstTime                         # not first time, move my marker
      Me.my_marker.moveMarker( Position.latitude, Position.longitude )
  ,
  init: ->
    # set again node:
    Map.node = document.getElementById('map')
    Map.map = new google.maps.Map( Map.node, Map.mapOptions )
}

Geoposition = {
  logged_in: false,
  check_logged_in: ->
    if document.getElementById('authenticated')
      this.logged_in = true
  putMapAndPreferences: ->
    # console.log("ok")
  init: ->
    this.check_logged_in()
    if this.logged_in
      Geo.init()
}



jQuery ->
  if document.getElementById 'authenticated'
    Me.logged_in = true
  Me.init()